package com.code.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.code.domain.StringUtils;
import com.code.entity.User;
import com.code.mapper.UserMapper;
import com.code.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.code.util.RSAUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.*;

/**
 * <p>
 * 商城用户信息表 服务实现类
 * </p>
 *
 * @author guanfenga@yonyou.com
 * @since 2022-08-23
 */
@Service
@Transactional
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate redisTemplate;



    @Override
    public List<User> queryUserList(User user) {
        LambdaQueryWrapper<User> wrapper = new QueryWrapper<User>().lambda();
        return userMapper.selectList(wrapper);
    }

    @Override
    public List<User> findUserPage(Integer page, Integer limit, User user) {
        return userMapper.findUserPage(page,limit,user);
    }

    @Override
    public int addUserLayUi(User user) throws Exception {
        String uuid = UUID.randomUUID().toString().replace("-","");
        user.setUserid(uuid);
        user.setCreatetime(new Date());
        user.setState(1);
        user.setAuditstate(0);
        if(user.getPassword() != null ){
            //加密操作
            user.setPassword(RSAUtil.getMd5(user.getPassword().trim()));
        }
        int result = 0 ;
        if(user != null){
          result = userMapper.insert(user);
        }
        return result ;
    }

    /**
     * 校验用户信息
     * @param username 用户名
     * @param pass 密码
     * @return
     */
    @Override
    public User checkedUserAndPass(String username, String pass) {

        return userMapper.checkedUserAndPass(username,pass);
    }

    /**
     * 根据id 进行删除造作
     * @param userid
     * @return
     */
    @Override
    public int deleteUserByuserid(String userid) {
        return userMapper.deleteById(userid);
    }

    @Override
    public User queryUserByUserId(String userid) {
        return userMapper.selectById(userid);
    }

    @Override
    public int updateUser(User user) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        if(user.getPassword() != null ){
            user.setPassword(RSAUtil.getMd5(user.getPassword().trim()));
        }
        return userMapper.updateById(user);
    }

    /**
     * 查询所有用户信息用于React 框架
     * @return
     */
    @Override
    public List<User> queryBasicUserAll() {
        //查询redis 中的内容是否存在
//        List<User> userList = (List<User>) redisTemplate.opsForValue().get("userList");
//        if( userList == null){
            List<User> userList = userMapper.queryBasicUserAll();
            List<User> users = new ArrayList<>();
            for(User user : userList){
                user.setKey(user.getUserid());
                users.add(user);
            }
//        }
        return users;
    }

    /**
     * 刷新用户信息
     * @return
     */
    @Override
    public Map<String,Object> getRefreshUser() {
        Map<String,Object> map = new HashMap<>();
       //查询所有用户信息
        List<User> userList = userMapper.queryBasicUserAll();
        if(userList != null){
            Boolean flage = redisTemplate.delete("brandList");
            if(flage){
                redisTemplate.opsForValue().set("userList",userList);
                map.put("success", true);
                map.put("data",userList.size());
            }
            else{
                map.put("success",false);
                map.put("data",0);
            }
        }
        return map;
    }

    /**
     * 根据用户名称进行查询
     * @param userName
     * @return
     */
    @Override
    public User queryUserName(String userName) {
        return userMapper.queryUserName(userName);
    }

    /**
     * 用户登录
     * @param user
     * @return
     */
    @Override
    public boolean login(User user) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        String username = user.getUsername();
        //加密用户密码
        String password  = RSAUtil.getMd5(user.getPassword().trim());
        //查询用户列表用于比对
        User users =  userMapper.checkedUserAndPass(username,password);
        if(users != null ){
            return true;
        }
        return false;
    }


}
